package com.sam.im.heiqun.uis.activities;

import android.content.Intent;
import android.os.Bundle;
import android.text.InputFilter;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.sam.im.heiqun.R;
import com.sam.im.heiqun.entities.GetmoneyEntivity;
import com.sam.im.heiqun.entities.PayTransferEntivity;
import com.sam.im.heiqun.nets.PGService;
import com.sam.im.heiqun.utils.CashierInputFilter;
import com.sam.im.heiqun.utils.RequestRSAUtils;
import com.sam.im.heiqun.utils.ToolsUtils;
import com.yuyh.library.nets.callbacks.AbsAPICallback;
import com.yuyh.library.nets.exceptions.ApiException;
import com.yuyh.library.uis.activitys.BaseSwipeBackActivity;

import butterknife.BindView;
import butterknife.OnClick;
import com.sam.im.heiqun.utils.StringUtils;

public class SetAmoutActivity extends BaseSwipeBackActivity {


    @BindView(R.id.pre_v_back)
    ImageView preVBack;
    @BindView(R.id.ed_amout)
    EditText edAmout;
    @BindView(R.id.bt_sure)
    Button btSure;
    @BindView(R.id.tv_describe)
    TextView tvDescribe ;
    private String key;
    private PGService pgService;

    @Override
    public int getContentViewId() {
        return R.layout.activity_set_amout;
    }

    @Override
    protected String getTitleText() {
        return getResources().getString(R.string.set_money);
    }

    @Override
    protected void initViews(Bundle savedInstanceState) {

        InputFilter[] filters = {new CashierInputFilter()};//限定输入为金额模式
        edAmout.setFilters(filters);
        pgService = PGService.getInstance();
        key = getIntent().getStringExtra("key");
        if (key != null) {
            if (key.equals("pay")){
                tvDescribe.setText(getResources().getString(R.string.pay_money_e));
            }else if (key.equals("get")){
                tvDescribe.setText(getResources().getString(R.string.get_money_e));
            }
        }
    }

    @OnClick({R.id.pre_v_back, R.id.bt_sure})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.pre_v_back:
                finish();
                break;
            case R.id.bt_sure:
                String trim = edAmout.getText().toString().trim();
                if (!StringUtils.isEmpty(trim)){
                    degetmoney(trim);
                }else {
                    ToolsUtils.showToast(SetAmoutActivity.this, getResources().getString(R.string.input_money_nonull));
                }
                break;
        }
    }

    private void degetmoney(String amout) {
        switch (key){
            case "pay":
                String userId=ToolsUtils.getMyUserId();
                com.alibaba.fastjson.JSONObject json = new JSONObject();
                json.put("userId", userId);
                json.put("amount", amout);
                String content = RequestRSAUtils.setRsaContent(json);
                pgService.paytransfer(content).subscribe(new AbsAPICallback<PayTransferEntivity>() {
                //pgService.paytransfer(amout,ToolsUtils.getMyUserId()).subscribe(new AbsAPICallback<PayTransferEntivity>() {
                    @Override
                    protected void onResultError(ApiException ex) {
                        ToolsUtils.showToast(SetAmoutActivity.this,getResources().getString(R.string.caozuo_fail));
                    }

                    @Override
                    public void onNext(PayTransferEntivity payTransferEntivity) {
                        if (payTransferEntivity != null) {
                            Log.i("info","pay=="+payTransferEntivity.toString());
                            Intent intent  = new Intent(SetAmoutActivity.this,QRcode.class);
                            intent.putExtra("paymoney",payTransferEntivity);
                            intent.putExtra("type",-11);
                            startActivity(intent);
                        }
                    }
                });
                break;
            case "get":
                String userId1=ToolsUtils.getMyUserId();
                com.alibaba.fastjson.JSONObject json1 = new JSONObject();
                json1.put("userId", userId1);
                json1.put("amount", amout);
                String content1 = RequestRSAUtils.setRsaContent(json1);
                pgService.getmoney(content1).subscribe(new AbsAPICallback<GetmoneyEntivity>() {
                    @Override
                    protected void onResultError(ApiException ex) {
                        ToolsUtils.showToast(SetAmoutActivity.this,getResources().getString(R.string.caozuo_fail));
                    }

                    @Override
                    public void onNext(GetmoneyEntivity getmoneyEntivity) {

                        if (getmoneyEntivity != null) {
                            Intent intent  = new Intent(SetAmoutActivity.this,QRcode.class);
                            intent.putExtra("getmoney",getmoneyEntivity);
                            intent.putExtra("type",-1);
                            startActivity(intent);
                        }
                    }
                });
                break;
        }

    }
}
